package com.xqdok.wdj.model;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xqdok.wdj.sqllite.SqlLiteUtils;

public class YonghuUtil {
	private SqlLiteUtils sqlLiteUtils;

	public YonghuUtil(Context context) {
		super();
		this.sqlLiteUtils = new SqlLiteUtils(context);
	}

	public int saveYonghu(Yonghu yonghu) {
		SQLiteDatabase database = sqlLiteUtils.getWritableDatabase();

		ContentValues contentValues = new ContentValues();
		contentValues.put("imei", yonghu.getImei());
		contentValues.put("username", yonghu.getUsername());
		contentValues.put("password", yonghu.getPassword());

		database.insert("yonghu", null, contentValues);
		database.close();
		return 1;
	}

	public List<Yonghu> queryYonghu() {
		SQLiteDatabase database = sqlLiteUtils.getReadableDatabase();

		List<Yonghu> yonghuList = new ArrayList<Yonghu>();
		Yonghu yh;

		String sql = " select * from yonghu where 1=1";
		Cursor cursor = database.rawQuery(sql, null);
		if (cursor.moveToNext()) {
			yh = new Yonghu();
			yh.setUsername(cursor.getString(cursor.getColumnIndex("username")));
			yh.setPassword(cursor.getString(cursor.getColumnIndex("password")));
			yh.setImei(cursor.getString(cursor.getColumnIndex("imei")));
			yonghuList.add(yh);
		}
		cursor.close();
		database.close();
		return yonghuList;
	}
}
